<%@ page import="schoolsource.sql.SchoolDBSQL" %>
<%@ page import="java.text.*" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.*" %>

<%@ page import="schoolsource.model.*" %>
<%@ page import="schoolsource.gui.FormFields" %>
<%@ page import="schoolsource.security.SecurityToken" %>
<% SecurityToken st = (SecurityToken) request.getAttribute(FormFields.SESSION);
   String requestObject = (String) st.getValue(FormFields.TOKEN);
   int classnum = (new Integer((String) st.getValue(FormFields.CLASSNUM))).intValue();
   ClassContainer classC[] = (ClassContainer[]) st.getValue("classContainer");
   UserContainer studentC[] = (UserContainer[]) st.getValue("studentsInClass");
   String loginType = (String) request.getAttribute(FormFields.USRTYPE); 
   String schoolName = (String) request.getAttribute(FormFields.SCHOOLNAME);
   GregorianCalendar gc[] = new GregorianCalendar[5];
   
   GregorianCalendar today = new GregorianCalendar();
   int dayNum;
   if (today.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
    dayNum = Calendar.SUNDAY + 5;
   }
   else {
    dayNum = today.get(Calendar.DAY_OF_WEEK) - 2;
   }
   for (int i = 0; i < gc.length; ++i) {
    gc[i] = new GregorianCalendar();
    gc[i].add(Calendar.DAY_OF_YEAR, i - dayNum);
   }
   
   SimpleDateFormat sdfMMDD = new SimpleDateFormat("MM-dd");
//   SimpleDateFormat sdfMMDDYYYY = new SimpleDateFormat("MM/dd/yyyy");
   SimpleDateFormat sdfMMDDYYYY = new SimpleDateFormat("MMddyyyy");

%>
   


<HTML>
  <BODY>
      <FORM NAME="classattendance" METHOD="POST">
    <TABLE ALIGN="CENTER" BORDER="0">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.CURRENT %>" VALUE="<%= FormFields.SUBFRAME %>">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.TOKEN %>" VALUE="<%= requestObject%>">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.PARAMETER %>" VALUE="">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.LINKCLICKED %>" VALUE="">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.CLASSNUM %>" VALUE="<%= classnum %>">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.SCHOOLNAME %>" VALUE="<%= schoolName %>">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.USRTYPE %>" VALUE="<%= loginType %>">
      <TR>
        <TD>
          <CENTER><INPUT TYPE="SUBMIT" NAME="<%= FormFields.BUTTONCLICKED %>" VALUE="Save Attendance">
        </TD>
        <TD>
          <CENTER><INPUT TYPE="SUBMIT" NAME="<%= FormFields.BUTTONCLICKED %>" VALUE="Cancel"></CENTER>
        </TD>
      </TR>
    </TABLE>
&nbsp;<br>
    <TABLE ALIGN="CENTER" BORDER="2">
      <TR border="2">
        <TH></TH>
        <TH width=100>Mon</TH>
        <TH width=100>Tue</TH>
        <TH width=100>Wed</TH>
        <TH width=100>Thu</TH>
        <TH width=100>Fri</TH>
      </TR>
      <TR>
        <TH></TH>
      <%
        for (int i = 0; i < gc.length; ++i) {
            out.print("<TH>"+ sdfMMDD.format(gc[i].getTime()) + "</TH>\n");
        }
      %>
      </TR>
        <%
        if (studentC != null) {
            for (int i = 0; i < studentC.length; i++) {
  //          for (int i = 0; i < 3; i++) {
                out.println("<TR>");
                out.println("<TD WIDTH=\"150\">" + studentC[i].getLastName() + ", " + studentC[i].getFirstName() + ": </TD>");

                String id = studentC[i].getAssignedID().toString();

                for (int j = 0; j < gc.length; j++) {

                    String time = sdfMMDDYYYY.format(gc[j].getTime());

                    AttendanceContainer ac = 
                        AttendanceContainer.getAttendanceContainer(id, 
                                           classC[classnum].getClassAssignedID().toString(), 
                                           time, 
                                           ((String) st.getValue(FormFields.SCHOOLNAME)));


//                    out.println("<TD><SELECT tabindex="+((i+1)+(j*studentC.length))+
//                        "; style=\"WIDTH: 100%;\" NAME=\"" +
//                        SchoolDBSQL.USRASSIGNEDID + id + 
//                        SchoolDBSQL.ATNDDATE + time + "\";>");
//                    if ("ABSENT".equals(ac.getType().toString())) {
//                        out.println("<OPTION VALUE=\"\">");
//                        out.println("<OPTION VALUE=\"ABSENT\" SELECTED>Absent");
//                        out.println("<OPTION VALUE=\"TARDY\">Tardy");
//                    }
//                    else if ("TARDY".equals(ac.getType().toString())) {
//                        out.println("<OPTION VALUE=\"\">");
//                        out.println("<OPTION VALUE=\"ABSENT\">Absent");
//                        out.println("<OPTION VALUE=\"TARDY\" SELECTED>Tardy");
//                    }
//                    else {
//                        out.println("<OPTION VALUE=\"\" SELECTED>");
//                        out.println("<OPTION VALUE=\"ABSENT\">Absent");
//                        out.println("<OPTION VALUE=\"TARDY\">Tardy");
//                    }
//                    out.println("</SELECT></TD>");
out.println("<TD><CENTER>");
out.println("<INPUT TYPE=\"RADIO\" NAME=\"" + SchoolDBSQL.USRASSIGNEDID + id + SchoolDBSQL.ATNDDATE + time + "\" VALUE=\" \"" + ("".equals(ac.getType().toString())?" CHECKED":"") + ">");
out.println("<INPUT TYPE=\"RADIO\" NAME=\"" + SchoolDBSQL.USRASSIGNEDID + id + SchoolDBSQL.ATNDDATE + time + "\" VALUE=\"ABSENT\"" + ("ABSENT".equals(ac.getType().toString())?" CHECKED":"") + ">");
out.println("<INPUT TYPE=\"RADIO\" NAME=\"" + SchoolDBSQL.USRASSIGNEDID + id + SchoolDBSQL.ATNDDATE + time + "\" VALUE=\"TARDY\"" + ("TARDY".equals(ac.getType().toString())?" CHECKED":"") + ">");
out.println("</CENTER></TD>");
                }
                out.println("</TR>");
            } 
        }
        %>
    </TABLE>
    &nbsp;<br>
    <TABLE ALIGN="CENTER" BORDER="0">
      <TR>
        <TD>
          <CENTER><INPUT TYPE="SUBMIT" NAME="<%= FormFields.BUTTONCLICKED %>" VALUE="Save Attendance">
        </TD>
        <TD>
          <CENTER><INPUT TYPE="SUBMIT" NAME="<%= FormFields.BUTTONCLICKED %>" VALUE="Cancel"></CENTER>
        </TD>
      </TR>
    </TABLE>
      </FORM>
  </BODY>
</HTML>